package MapReduce.Demo9_Grouping_Gender;

import MapReduce.writableBean.GenderWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * @Author lixinlei
 * @Date 2023/3/21 11:22
 */
public class GenderGroupingComparator extends WritableComparator {

    public GenderGroupingComparator(){
        super(GenderWritable.class,true);
    }


    @Override
    public int compare(WritableComparable a, WritableComparable b) {

        GenderWritable aBean = (GenderWritable) a;
        GenderWritable bBean = (GenderWritable) b;

        String aGroupName = aBean.getGroupName();
        String bGroupName = bBean.getGroupName();

        int aAge = aBean.getAge();
        int bAge = bBean.getAge();

        if(aGroupName.equals(bGroupName)){
            return aAge-bAge;
        }else{
            return aGroupName.compareTo(bGroupName);
        }

    }
}
